package com.soujiayi.util;

import java.text.ParseException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.soujiayi.weibo.User;

public class UserKeeper {

	/**
	 * 保存User到SharedPreferences
	 * 
	 * @param context
	 *            Activity 上下文环境
	 * @param token
	 *            Oauth2AccessToken
	 */
	public static void keepUser(Context context, User user, String name) {
		SharedPreferences pref = context.getSharedPreferences(name,
				Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.putInt("type", user.getType());
		editor.putInt("uid", user.getUid());
		editor.putString("username", user.getUsername());
		editor.putString("nickname", user.getNickname());
		editor.putString("password", user.getPassword());
		editor.putString("email", user.getEmail());
		editor.putString("gender", user.getGender());
		editor.putString("birthday", user.getBirthday());
		editor.putString("cityid", user.getCityId());
		editor.putString("cityname", user.getCityName());
		editor.putString("avatar_url",user.getAvatar_url());
		editor.putInt("status", user.getStatus());

		editor.putString("sina_token", user.getSina_token());
		editor.putString("sina_expires_in", user.getSina_expires_in());
		editor.putString("sina_expires_time", user.getSina_expires_time());

		editor.putString("tencent_token", user.getTencent_token());
		editor.putString("tencent_expires_in", user.getTencent_expires_in());
		editor.putString("tencent_expires_time", user.getTencent_expires_time());
		editor.putString("tencent_clientId", user.getTencent_clientId());
		editor.putString("tencent_clientIp", user.getTencent_clientIp());
		editor.putString("tencent_clientSecret", user.getTencent_clientSecret());
		editor.putString("tencent_oauthVersion", user.getTencent_oauthVersion());
		editor.putString("tencent_openid", user.getTencent_openid());
		editor.putString("tencent_openkey", user.getTencent_openkey());
		editor.putString("tencent_redirectUri", user.getTencent_redirectUri());
		editor.putString("tencent_scope", user.getTencent_scope());
		editor.commit();
	}

	public static void updateUser(Context context, String name, String avatar_url, String nickName , String gender, String cityId,String cityName,String birthday) {
		SharedPreferences pref = context.getSharedPreferences(name,
				Context.MODE_APPEND);
		Editor editor = pref.edit();
		if(avatar_url!= null && !"".equals(avatar_url))
			editor.putString("avatar_url", avatar_url);
		if(nickName!= null && !"".equals(nickName))
			editor.putString("nickname", nickName);
		if(gender!= null && !"".equals(gender))
			editor.putString("gender", gender);
		if(birthday!= null && !"".equals(birthday))
		editor.putString("birthday", birthday);
		if(cityId!= null && !"".equals(cityId))
			editor.putString("cityid", cityId);
		if(cityName!= null && !"".equals(cityName))
			editor.putString("cityname", cityName);
		editor.commit();
	}

	
	/**
	 * 清空sharepreference
	 * 
	 * @param context
	 */
	public static void clear(Context context) {
		SharedPreferences pref = context.getSharedPreferences(Constants.PREFERENCES_NAME_USER,
				Context.MODE_APPEND);
		if(pref != null){
			Editor editor = pref.edit();
			editor.clear();
			editor.commit();
		}
		
		SharedPreferences pref_sina = context.getSharedPreferences(Constants.PREFERENCES_NAME_USER_SINA,
				Context.MODE_APPEND);
		if(pref_sina != null){
			Editor editor = pref_sina.edit();
			editor.clear();
			editor.commit();
		}
		
		SharedPreferences pref_tencent = context.getSharedPreferences(Constants.PREFERENCES_NAME_USER_TENCENT,
				Context.MODE_APPEND);
		if(pref_tencent != null){
			Editor editor = pref_tencent.edit();
			editor.clear();
			editor.commit();
		}
		
	}
	
	public static void clear(Context context, String name) {
		SharedPreferences pref = context.getSharedPreferences(name,
				Context.MODE_APPEND);
		if(pref != null){
			Editor editor = pref.edit();
			editor.clear();
			editor.commit();
		}
	}

	/**
	 * 从SharedPreferences读取User
	 * 
	 * @param context
	 * @return User
	 * @throws ParseException
	 */
	public static User readUser(Context context, String name) {	
		SharedPreferences pref = context.getSharedPreferences(name,
				Context.MODE_APPEND);
		if(pref == null){
			return null;
		}
		
		if(name.equals(Constants.PREFERENCES_NAME_USER)){
			if(pref.getInt("uid", 0) == 0){
				return null;
			}
		}
		if(name.equals(Constants.PREFERENCES_NAME_USER_SINA)){
			if(pref.getString("sina_token", "") == null || pref.getString("sina_token", "").equals("")){
				return null;
			}
		}
		if(name.equals(Constants.PREFERENCES_NAME_USER_TENCENT)){
			if(pref.getString("tencent_token", "") == null || pref.getString("tencent_token", "").equals("")){
				return null;
			}
		}
		
		User user = new User();
		user.setType(pref.getInt("type", 3));
		user.setUid(pref.getInt("uid", 0));
		user.setUsername(pref.getString("username", ""));
		user.setNickname(pref.getString("nickname", ""));
		user.setPassword(pref.getString("password", ""));
		user.setEmail(pref.getString("email", ""));
		user.setGender(pref.getString("gender", ""));
		user.setBirthday(pref.getString("birthday", ""));
		user.setCityId(pref.getString("cityid", ""));
		user.setCityName(pref.getString("cityname", ""));
		user.setAvatar_url(pref.getString("avatar_url", ""));
		user.setStatus(pref.getInt("status", 0));
		
		user.setSina_token(pref.getString("sina_token", ""));
		user.setSina_expires_in(pref.getString("sina_expires_in", ""));
		user.setSina_expires_time(pref.getString("sina_expires_time", ""));

		user.setTencent_token(pref.getString("tencent_token", ""));
		user.setTencent_expires_in(pref.getString("tencent_expires_in", ""));
		user.setTencent_expires_time(pref.getString("tencent_expires_time", ""));
		user.setTencent_clientId(pref.getString("tencent_clientId", ""));
		user.setTencent_clientIp(pref.getString("tencent_clientIp", ""));
		user.setTencent_clientSecret(pref.getString("tencent_clientSecret", ""));
		user.setTencent_oauthVersion(pref.getString("tencent_oauthVersion", ""));
		user.setTencent_openid(pref.getString("tencent_openid", ""));
		user.setTencent_openkey(pref.getString("tencent_openkey", ""));
		user.setTencent_redirectUri(pref.getString("tencent_redirectUri", ""));
		user.setTencent_scope(pref.getString("tencent_scope", ""));
		return user;
	}
}
